//
//  QJOrderDetailVC.m
//  QuanJieShopProject
//
//  Created by SunChao on 2018/6/20.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJOrderDetailVC.h"
#import "QJPreviewOrderVCHeaderView.h"
#import "QJPreviewOrderCell.h"
#import "DetailOrderBottomCell.h"

@interface QJOrderDetailVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)QJPreviewOrderVCHeaderView *headerView;
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)OrderDetailModel *model;
@property (nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation QJOrderDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情";
    self.tbView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT) style:UITableViewStylePlain];
    [self.tbView registerNib:[UINib nibWithNibName:@"QJPreviewOrderCell" bundle:nil] forCellReuseIdentifier:@"QJPreviewOrderCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"DetailOrderBottomCell" bundle:nil] forCellReuseIdentifier:@"DetailOrderBottomCell"];
    self.tbView.dataSource = self;
    self.tbView.delegate = self;
    self.tbView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.tbView];
    self.headerView = [[[NSBundle mainBundle]loadNibNamed:@"QJPreviewOrderVCHeaderView" owner:self options:nil] lastObject];
    self.headerView.frame = CGRectMake(0, 0, UISCREEN_WIDTH, 80);
    self.tbView.tableHeaderView = self.headerView;
    self.headerView.userInteractionEnabled = NO;
    self.tbView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self requestData];
    
}

- (void)requestData{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.orderID forKey:@"id"];
    [WWNetRequest fetchOrderDetailWithParameters:dict success:^(id response) {
        if (response) {
            NSDictionary *dic = response[@"0"][@"order_info"];
            OrderDetailModel *model = [[OrderDetailModel alloc]initWithDictionary:dic error:nil];
           NSArray *array =  [dic objectForKey:@"goods_list"];
              self.model = model;
            if (array.count>0) {
                self.model.image_url =  [[array objectAtIndex:0]objectForKey:@"image_url"];
                self.model.goods_num =[[array objectAtIndex:0]objectForKey:@"goods_num"];
                self.headerView.addressLabel.text = [NSString stringWithFormat:@"%@%@\n%@%@%@%@",self.model.consignee,self.model.mobile,self.model.province_show,self.model.city_show,self.model.address,self.model.region_show];
                for (NSDictionary *dict in array) {
                     OrderDetailModel *model = [[OrderDetailModel alloc]initWithDictionary:dict error:nil];
                     [self.dataSource addObject:model];
                }
            }
           
            [self.tbView reloadData];
        }
        
    } failure:^(NSDictionary *error) {
        
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   
    if (indexPath.row == self.dataSource.count) {
        return 180;
    }
    
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   
    if (indexPath.row <self.dataSource.count) {
        
        QJPreviewOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QJPreviewOrderCell"];
        cell.userInteractionEnabled = NO;
       OrderDetailModel *model = [self.dataSource objectAtIndex:indexPath.row];
        [cell.cellImage sd_setImageWithURL:[NSURL URLWithString:model.image_url] placeholderImage:[UIImage imageNamed:@"noImage"]];
        cell.cellTitle.text = model.goods_name;
        cell.cellContent.text = [NSString stringWithFormat:@"数量:%@",model.goods_num];
        cell.cellPrice.text = model.goods_price;
        
        return cell;
        
    }else{
        
        DetailOrderBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailOrderBottomCell"];
        cell.userInteractionEnabled = NO;
        cell.yunfeiLabel.text = self.model.shipping_price;
        cell.moneyLabel.text = self.model.total_amount;
        cell.orderLabel.text = self.model.order_sn;
        cell.timeLabel.text = [self timeStringChangeToTime:self.model.add_time];
       
        return cell;
        
    }
}

- (NSString *)timeStringChangeToTime:(NSString *)time{
    // timeStampString 是服务器返回的13位时间戳
    
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[time doubleValue] / 1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString       = [formatter stringFromDate: date];
    NSLog(@"服务器返回的时间戳对应的时间是:%@",dateString);
    return dateString;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count+1;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0;
}

ARRAY_INIT(dataSource);
@end
